import { getList as _getList } from './api';
import { Dispatch } from 'redux';
import type { IListParam, IStateProp } from './index.d'

export const MODULE_NAME: string = 'BasicApp/FacultyInfo';

export const types = {
  TABLE_DATA: `${MODULE_NAME}/TABLE_DATA`, // 列表数据
}

export default (state: IStateProp = {
  tableData: {
    total: 0,
    dataSource: []
  }
}, action: any) => {
  switch (action.type) {
    case types.TABLE_DATA:
      return Object.assign({}, state, {
        tableData: action.data
      })
    default: return state
  }
}

/**
 * 获取列表数据
*/
export const getList = (params: IListParam) => {
  return async (dispatch: Dispatch): Promise<void> => {
    try {
      const res = await _getList(params);
      dispatch({
        type: types.TABLE_DATA,
        data: {
          dataSource: res.data.content,
          page: params.page,
          limit: params.limit,
          total: res.data.counts
        }
      })
    } catch (err) {}
  }
}
